﻿
$(function(){
	var pathName=window.document.location.pathname;
	var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
	var loginUserId = $("#loginUserId").text();
	
	//动态展现echarts图表
	window.setTimeout(function(){
		$.ajax({
			url:projectName + "/WrongWord/selectWrongWordOfTopTen",
			type:"post",
			async:true,
			data:{
				"userId" : loginUserId
			},
			dataType:"json",
			success:function (data) {
				var myChart = echarts.init(document.getElementById('echarts'));
				var xData = [];
				var yData = [];
				for(var i = 0; i <data.length; i++) {
					xData[i] = data[i].word.wordChinese;
					yData[i] = data[i].wrongCount;
				}
		        // 指定图表的配置项和数据
		        var option = {
		            title: {
		                text: '错误单词排行前二十'
		            },
		            color: ['#3398DB'],
		            tooltip : {
		                trigger: 'axis',
		                axisPointer : {            // 坐标轴指示器，坐标轴触发有效
		                    type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
		                }
		            },
		            grid: {
		                left: '3%',
		                right: '4%',
		                bottom: '3%',
		                containLabel: true
		            },
		            xAxis : [
		                {
		                    type : 'category',
		                    data : xData,
		                    axisTick: {
		                        alignWithLabel: true
		                    }
		                }
		            ],
		            yAxis : [
		                {
		                    type : 'value'
		                }
		            ],
		            series : [
		                {
		                    name:'错误次数',
		                    type:'bar',
		                    barWidth: '60%',
		                    data:yData
		                }
		            ]
		        };

		        // 使用刚指定的配置项和数据显示图表。
		        myChart.setOption(option);
			}
		});
	},500);
	
	
	function load() {
		setTimeout(function() {
			$('#pageCount').on('change', function() {
				var pageCountValue = $('#pageCount').val();
				var nowPageNumberValue = $('#nowPageNumber').text();
				$.ajax({
					url:projectName + "/Word/pageCountChange",
					type:"post",
					async:true,
					data:{
						"pageCount" : pageCountValue,
						"nowPageNumber" : nowPageNumberValue
					},
					dataType:"json",
					success:function (data) {
						$('#dataShow').empty();
						for(var i = 0; i <data.length; i++) {
							let wordId = data[i].wordId;
							let wordChinese = data[i].wordChinese;
							let wordPing = data[i].wordPing;
							let wordPian = data[i].wordPian;
							let wordWord = data[i].wordWord;
							let wordContend = data[i].wordContend;
							let wordSexName = data[i].wordSex.wordSexName;
							let gradeName = data[i].grade.gradeName;
							if(wordPing == null){
								wordPing = "";
							}
							if(wordPian == null){
								wordPian = "";
							}
							if(wordWord == null){
								wordWord = "";
							}
							if(wordContend == null){
								wordContend = "";
							}
							let $_tr = $('<tr></tr>');
							let $_td1 = $('<td>' + wordChinese + '</td>');
							let $_td2 = $('<td>' + wordPing + '</td>');
							let $_td3 = $('<td>' + wordPian + '</td>');
							let $_td4 = $('<td>' + wordWord + '</td>');
							let $_td5 = $('<td>' + wordContend + '</td>');
							let $_td8 = $('<td>' + wordSexName + '</td>');
							let $_td9 = $('<td>' + gradeName + '</td>');
							$_tr.append($_td1);
							$_tr.append($_td2);
							$_tr.append($_td3);
							$_tr.append($_td4);
							$_tr.append($_td8);
							$_tr.append($_td9);
							$_tr.append($_td5);
							let $_td6 = $('<td class="text-center"></td>');
							let $_a1 = $('<a href="' + projectName + '/WrongWord/updateMaster?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
							let $_button1 = $('<button class="btn btn-danger">掌握</button>');
							$_a1.append($_button1);
							$_td6.append($_a1);
							$_tr.append($_td6);
							$('#dataShow').append($_tr);
						};
					}
				});
				setTimeout(load(),1);
			});
			
			$('.pageNumber').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var toPageNumberValue = $(this).text();
				$.ajax({
					url:projectName + "/Word/toPage",
					type:"post",
					async:true,
					data:{
						"pageCount" : pageCountValue,
						"toPageNumber" : toPageNumberValue
					},
					dataType:"json",
					success:function (data) {
						$('#dataShow').empty();
						for(var i = 0; i <data.length; i++) {
							let wordId = data[i].wordId;
							let wordChinese = data[i].wordChinese;
							let wordPing = data[i].wordPing;
							let wordPian = data[i].wordPian;
							let wordWord = data[i].wordWord;
							let wordContend = data[i].wordContend;
							let wordSexName = data[i].wordSex.wordSexName;
							let gradeName = data[i].grade.gradeName;
							if(wordPing == null){
								wordPing = "";
							}
							if(wordPian == null){
								wordPian = "";
							}
							if(wordWord == null){
								wordWord = "";
							}
							if(wordContend == null){
								wordContend = "";
							}
							let $_tr = $('<tr></tr>');
							let $_td1 = $('<td>' + wordChinese + '</td>');
							let $_td2 = $('<td>' + wordPing + '</td>');
							let $_td3 = $('<td>' + wordPian + '</td>');
							let $_td4 = $('<td>' + wordWord + '</td>');
							let $_td5 = $('<td>' + wordContend + '</td>');
							let $_td8 = $('<td>' + wordSexName + '</td>');
							let $_td9 = $('<td>' + gradeName + '</td>');
							$_tr.append($_td1);
							$_tr.append($_td2);
							$_tr.append($_td3);
							$_tr.append($_td4);
							$_tr.append($_td8);
							$_tr.append($_td9);
							$_tr.append($_td5);
							let $_td6 = $('<td class="text-center"></td>');
							let $_a1 = $('<a href="' + projectName + '/WrongWord/updateMaster?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
							let $_button1 = $('<button class="btn btn-danger">掌握</button>');
							$_a1.append($_button1);
							$_td6.append($_a1);
							$_tr.append($_td6);
							$('#dataShow').append($_tr);
						};
					}
				});
				setTimeout(load(),1);
			});
			
			$('#prevPage').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var nowPageNumberValue = $('#nowPageNumber').text();
				var toPageNumberValue = nowPageNumberValue;
				if(toPageNumberValue >= 1) {
					$.ajax({
						url:projectName + "/Word/toPage",
						type:"post",
						async:true,
						data:{
							"pageCount" : pageCountValue,
							"toPageNumber" : toPageNumberValue
						},
						dataType:"json",
						success:function (data) {
							$('#dataShow').empty();
							for(var i = 0; i <data.length; i++) {
								let wordId = data[i].wordId;
								let wordChinese = data[i].wordChinese;
								let wordPing = data[i].wordPing;
								let wordPian = data[i].wordPian;
								let wordWord = data[i].wordWord;
								let wordContend = data[i].wordContend;
								let wordSexName = data[i].wordSex.wordSexName;
								let gradeName = data[i].grade.gradeName;
								if(wordPing == null){
									wordPing = "";
								}
								if(wordPian == null){
									wordPian = "";
								}
								if(wordWord == null){
									wordWord = "";
								}
								if(wordContend == null){
									wordContend = "";
								}
								let $_tr = $('<tr></tr>');
								let $_td1 = $('<td>' + wordChinese + '</td>');
								let $_td2 = $('<td>' + wordPing + '</td>');
								let $_td3 = $('<td>' + wordPian + '</td>');
								let $_td4 = $('<td>' + wordWord + '</td>');
								let $_td5 = $('<td>' + wordContend + '</td>');
								let $_td8 = $('<td>' + wordSexName + '</td>');
								let $_td9 = $('<td>' + gradeName + '</td>');
								$_tr.append($_td1);
								$_tr.append($_td2);
								$_tr.append($_td3);
								$_tr.append($_td4);
								$_tr.append($_td8);
								$_tr.append($_td9);
								$_tr.append($_td5);
								let $_td6 = $('<td class="text-center"></td>');
								let $_a1 = $('<a href="' + projectName + '/WrongWord/updateMaster?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
								let $_button1 = $('<button class="btn btn-danger">掌握</button>');
								$_a1.append($_button1);
								$_td6.append($_a1);
								$_tr.append($_td6);
								$('#dataShow').append($_tr);
							};
						}
					});
				}
				setTimeout(load(),1);
			});
			
			$('#nextPage').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var nowPageNumberValue = $('#nowPageNumber').text();
				var toPageNumberValue = nowPageNumberValue;
				var countNum = $('#count').text();
				var maxPage = Math.ceil(countNum/pageCountValue);
				if(maxPage - toPageNumberValue >= 0) {
					$.ajax({
						url:projectName + "/Word/toPage",
						type:"post",
						async:true,
						data:{
							"pageCount" : pageCountValue,
							"toPageNumber" : toPageNumberValue
						},
						dataType:"json",
						success:function (data) {
							$('#dataShow').empty();
							for(var i = 0; i <data.length; i++) {
								let wordId = data[i].wordId;
								let wordChinese = data[i].wordChinese;
								let wordPing = data[i].wordPing;
								let wordPian = data[i].wordPian;
								let wordWord = data[i].wordWord;
								let wordContend = data[i].wordContend;
								let wordSexName = data[i].wordSex.wordSexName;
								let gradeName = data[i].grade.gradeName;
								if(wordPing == null){
									wordPing = "";
								}
								if(wordPian == null){
									wordPian = "";
								}
								if(wordWord == null){
									wordWord = "";
								}
								if(wordContend == null){
									wordContend = "";
								}
								let $_tr = $('<tr></tr>');
								let $_td1 = $('<td>' + wordChinese + '</td>');
								let $_td2 = $('<td>' + wordPing + '</td>');
								let $_td3 = $('<td>' + wordPian + '</td>');
								let $_td4 = $('<td>' + wordWord + '</td>');
								let $_td5 = $('<td>' + wordContend + '</td>');
								let $_td8 = $('<td>' + wordSexName + '</td>');
								let $_td9 = $('<td>' + gradeName + '</td>');
								$_tr.append($_td1);
								$_tr.append($_td2);
								$_tr.append($_td3);
								$_tr.append($_td4);
								$_tr.append($_td8);
								$_tr.append($_td9);
								$_tr.append($_td5);
								let $_td6 = $('<td class="text-center"></td>');
								let $_a1 = $('<a href="' + projectName + '/WrongWord/updateMaster?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
								let $_button1 = $('<button class="btn btn-danger">掌握</button>');
								$_a1.append($_button1);
								$_td6.append($_a1);
								$_tr.append($_td6);
								$('#dataShow').append($_tr);
							};
						}
					});
				}
				setTimeout(load(),1);
			});
			
			$('#goPageButton').on('click', function() {
				var pageCountValue = $('#pageCount').val();
				var toPageNumberValue = $('#goPageInput').val();
				var countNum = $('#count').text();
				var maxPage = Math.ceil(countNum/pageCountValue);
				if(maxPage - toPageNumberValue < 0) {
					toPageNumberValue = maxPage;
				}
				if(toPageNumberValue <= 0) {
					$('#goPageInput').val('1');
					toPageNumberValue = 1;
				}
				$.ajax({
					url:projectName + "/Word/toPage",
					type:"post",
					async:true,
					data:{
						"pageCount" : pageCountValue,
						"toPageNumber" : toPageNumberValue
					},
					dataType:"json",
					success:function (data) {
						$('#dataShow').empty();
						for(var i = 0; i <data.length; i++) {
							let wordId = data[i].wordId;
							let wordChinese = data[i].wordChinese;
							let wordPing = data[i].wordPing;
							let wordPian = data[i].wordPian;
							let wordWord = data[i].wordWord;
							let wordContend = data[i].wordContend;
							let wordSexName = data[i].wordSex.wordSexName;
							let gradeName = data[i].grade.gradeName;
							if(wordPing == null){
								wordPing = "";
							}
							if(wordPian == null){
								wordPian = "";
							}
							if(wordWord == null){
								wordWord = "";
							}
							if(wordContend == null){
								wordContend = "";
							}
							let $_tr = $('<tr></tr>');
							let $_td1 = $('<td>' + wordChinese + '</td>');
							let $_td2 = $('<td>' + wordPing + '</td>');
							let $_td3 = $('<td>' + wordPian + '</td>');
							let $_td4 = $('<td>' + wordWord + '</td>');
							let $_td5 = $('<td>' + wordContend + '</td>');
							let $_td8 = $('<td>' + wordSexName + '</td>');
							let $_td9 = $('<td>' + gradeName + '</td>');
							$_tr.append($_td1);
							$_tr.append($_td2);
							$_tr.append($_td3);
							$_tr.append($_td4);
							$_tr.append($_td8);
							$_tr.append($_td9);
							$_tr.append($_td5);
							let $_td6 = $('<td class="text-center"></td>');
							let $_a1 = $('<a href="' + projectName + '/WrongWord/updateMaster?user.userId=' + loginUserId + '&word.wordId=' + wordId + '"></a>');
							let $_button1 = $('<button class="btn btn-danger">掌握</button>');
							$_a1.append($_button1);
							$_td6.append($_a1);
							$_tr.append($_td6);
							$('#dataShow').append($_tr);
						};
					}
				});
				setTimeout(load(),1);
			});
		},100);
	};
	setTimeout(load(),1);
	
});